package com.itheima.edu.info.manager.controller;


import com.itheima.edu.info.manager.domain.Student;
import com.itheima.edu.info.manager.service.StudentService;

import java.util.Scanner;

//学生管理界面的展示
public class StudentController {
    StudentService studentservice =new StudentService();
    Scanner scanner = new Scanner(System.in);
    public void start(){
        //键盘录入
        //不断操作
        lo:
        while (true) {
            // 1. 搭建主界面菜单
            System.out.println("--------欢迎来到学生管理系统--------");
            System.out.println("1 添加学生");
            System.out.println("2 删除学生");
            System.out.println("3 修改学生");
            System.out.println("4 查看学生");
            System.out.println("5 退出");
            System.out.println("请输入您的选择:");
            String choice = scanner.next();

            switch (choice) {
                case "1":
                    //System.out.println("添加学生");
                    addStudent();
                    break;
                case "2":
                    //System.out.println("删除学生");
                    deleteStudentById();
                    break;
                case "3":
                  //  System.out.println("修改学生");
                    updateStudent();
                    break;
                case "4":
                    findAllStudent();
                     System.out.println("查看学生");
                  //  queryStudents(list);
                    break;
                case "5":
                    System.out.println("感谢您的使用");
                    break lo;
                default:
                    System.out.println("您的输入有误");
                    break;
            }
        }
        //
    }

    //修改学生信息
    private void updateStudent() {
        //1.调用业务员中的获取方法,得到学生的对象数组
        Student[] stus = studentservice.findAllStudent();

        if(stus==null){
            System.out.println("查无信息,请添加后重试");
            return;
        }
        String id =inputStudentId();
        int isExists = studentservice.isExists(id);
        Student s = inputStudentInfo(id);
        studentservice.updateStudent(isExists,s);
        System.out.println("删除成功!!");
    }

    //根据id删除学生
    private void deleteStudentById() {
        //1.调用业务员中的获取方法,得到学生的对象数组
        Student[] stus = studentservice.findAllStudent();

        if(stus==null){
            System.out.println("查无信息,请添加后重试");
            return;
        }
        //键盘录入学号
        String id =inputStudentId();
        int isExists = studentservice.isExists(id);
        studentservice.deleteStudentById(isExists);
        System.out.println("删除成功!");

    }

    //添加学生信息
    public void addStudent() {

        String id;
        while(true) {
            System.out.println("请输入学生id:");
            id = scanner.next();
            int isExists = studentservice.isExists(id);
            if(isExists==-1){
                break;
            }else{
                System.out.println("该账号已被占用!!");
            }
        }
        Student s = inputStudentInfo(id);
        boolean flag = studentservice.addStudent(s);
    }

    //查询数据
    public void findAllStudent(){
        //1.调用业务员中的获取方法,得到学生的对象数组
        Student[] stus = studentservice.findAllStudent();

        if(stus==null){
            System.out.println("查无信息,请添加后重试");
            return;
        }

        //3.遍历数组,获取学生信息打印在控制台
        System.out.println("学号\t\t姓名\t年龄\t生日");
        for (int i = 0; i < stus.length; i++) {
            Student stu = stus[i];
            if (stu != null) {
                System.out.println(stu.getId() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t\t" + stu.getBirthday());
            }
        }
    }

    //键盘录入信息
    public Student inputStudentInfo(String id) {
        System.out.println("请输入学生姓名:");
        String name = scanner.next();
        System.out.println("请输入学生年龄:");
        String age = scanner.next();
        System.out.println("请输入学生生日:");
        String birthday = scanner.next();

        Student s = new Student(id,name,age,birthday);
        return s;
    }

    // 键盘录入学生id
    public String inputStudentId() {
        String id;
        while (true) {
            System.out.println("请输入学生id:");
            id = scanner.next();
            int exists = studentservice.isExists(id);
            if (exists==-1) {
                System.out.println("您输入的id不存在, 请重新输入:");
            } else {
                break;
            }
        }
        return id;
    }


}
